package com.moxi.mogublog.sms.config;

import com.moxi.mogublog.sms.global.SysConf;
import org.springframework.amqp.core.*;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * RabbitMQ 配置
 *
 * @author LiShuai
 * @date 2020/5/27 10:08
 */
@Configuration
public class RabbitMqConfig {

    /**
     * 声明交换机
     */
    @Bean(SysConf.RM_EXCHANGE_DIRECT)
    public Exchange EXCHANGE_DIRECT() {
        // 声明路由交换机，durable:在rabbitmq重启后，交换机还在
        return ExchangeBuilder.directExchange(SysConf.RM_EXCHANGE_DIRECT).durable(true).build();
    }

    /**
     * 声明Blog队列
     *
     * @return
     */
    @Bean(SysConf.RM_MOGU_BLOG)
    public Queue MOGU_BLOG() {
        return new Queue(SysConf.RM_MOGU_BLOG);
    }

    /**
     * 声明Email队列
     *
     * @return
     */
    @Bean(SysConf.RM_MOGU_EMAIL)
    public Queue MOGU_EMAIL() {
        return new Queue(SysConf.RM_MOGU_EMAIL);
    }

    /**
     * 声明SMS队列
     *
     * @return
     */
    @Bean(SysConf.RM_MOGU_SMS)
    public Queue MOGU_SMS() {
        return new Queue(SysConf.RM_MOGU_SMS);
    }

    /**
     * mogu.blog 队列绑定交换机，指定routingKey
     *
     * @param queue
     * @param exchange
     * @return
     */
    @Bean
    public Binding BINDING_QUEUE_INFORM_BLOG(
            @Qualifier(SysConf.RM_MOGU_BLOG) Queue queue,
            @Qualifier(SysConf.RM_EXCHANGE_DIRECT) Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(SysConf.RM_ROUTING_KEY_BLOG).noargs();
    }

    /**
     * mogu.mail 队列绑定交换机，指定routingKey
     *
     * @param queue
     * @param exchange
     * @return
     */
    @Bean
    public Binding BINDING_QUEUE_INFORM_EMAIL(
            @Qualifier(SysConf.RM_MOGU_EMAIL) Queue queue,
            @Qualifier(SysConf.RM_EXCHANGE_DIRECT) Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(SysConf.RM_ROUTING_KEY_EMAIL).noargs();
    }

    /**
     * mogu.sms 队列绑定交换机，指定routingKey
     *
     * @param queue
     * @param exchange
     * @return
     */
    @Bean
    public Binding BINDING_QUEUE_INFORM_SMS(
            @Qualifier(SysConf.RM_MOGU_SMS) Queue queue,
            @Qualifier(SysConf.RM_EXCHANGE_DIRECT) Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(SysConf.RM_ROUTING_KEY_SMS).noargs();
    }

    @Bean
    public MessageConverter messageConverter() {
        return new Jackson2JsonMessageConverter();
    }

}
